import * as cc from "cc";
import { resourcesMgr } from "../../../framework/loader/ResourcesMgr";
import { UIBase } from '../../../framework/ui/UIBase';
import { DataUtils } from "../../../framework/vm/DataUtils";
import { VMCenter } from "../../../framework/vm/VMCenter";
import { VMEntity } from '../../../framework/vm/VMEntity';
import { VM } from "../../../framework/vm/VMMgr";
import { PlayerSKills, ConfigSkills } from "../../../userdata/StorageModel";
import { player } from "../../../userdata/UserModel";
const { ccclass, property } = cc._decorator;

/**
 * Predefined variables
 * Name = CellSkillIcon
 * DateTime = Sun Mar 20 2022 23:29:06 GMT+0800 (中国标准时间)
 * Author = 272493431
 * FileBasename = CellSkillIcon.ts
 * FileBasenameNoExtension = CellSkillIcon
 * URL = db://assets/scripts/scene/VMDemo/items/CellSkillIcon.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */
 
interface CellData {
    name:string;
    info:string;
    id:number;
    pic:number;
    level:number;
    max:number;
    unlock:number;
    condition:{
        skill:number, 
        point:number,
        all_use:number
    }
}

@ccclass('CellSkillIcon')
export class CellSkillIcon extends UIBase implements IMVVM{
    _vmTag: string;
    _vmEntitys: VMEntity[];
    private _index : number = 0;
    /**技能的数据库编号 */
    public get index() : number {
        return this._index;
    }
    public set index(v : number) {
        if(v<=-1) v =-1;
        let res =  this.updateData(v);
        if(res == true){
            this._index = v; 
        }

    }
    
    data: CellData = {
        name:'',
        info: '',
        id:0,
        pic: 0,
        level:0,
        max:0,
        unlock:0,
        condition:{
            skill: 0, 
            point:0,
            all_use:0
        }
    }
    private skill_point:number = 0;

    onLoaded(){
        let self = this;
        VMCenter.add(this.data,this);
        let icon = this.find("icon");
        let text = this.find("text");
        let point = this.find("point");
        let btn_add = this.find("btn_add");

        VMCenter.Node(this,text,{watchPath: "*.unlock", formator(opts){
            return opts.newValue === 0;
        }});

        VMCenter.UIOpacity(this,icon,{watchPath: "*.unlock", formator(opts){
            return opts.newValue === 0 ? 55 : 255;
        }});


        VMCenter.Label(this,point,{watchPath: ["*.level","*.max"],formator(opts){
            return opts.newValue;
        }});
        
        VMCenter.Node(this,btn_add,{watchPath: ['*.level',"*.max","*.unlock"], formator(opts){
            let [level,max,unlock] = opts.newValue;
            return level < max && self.skill_point > 0 && unlock === 1;
        }});

        
        this.registeButtonClick(btn_add,()=>{
               //升级技能点
                let p = PlayerSKills.find(data => data.id === this.index);
                if(p ==null)return false;
                if(player.skill_point<1)return;
                // 全局数据进行升级
                p.level +=1;
                player.skill_point -=1;

                //刷新数据，将全局数据合并到本类中
                this.updateData(this.index);
        })


        // TODO: VMCenter 新建一个 事件方法 可以不用传入 持有组件 可以被 VMCenter.remove 一并管理
        VM.on("player.skill_point",(val)=>{
            this.skill_point = val;
            this.index = this.index;
        },this);


        this.skill_point = player.skill_point;
    }

    onUnloaded(){
        VM.targetOff(this);
        VMCenter.remove(this);
    }

    updateData(v:number):boolean{
        let user = PlayerSKills.find(data => data.id === v);
        if(user ==null)return false;
        let skill = ConfigSkills.find(data => data.id === user.skillId);
        if(skill ==null)return false;


        let tmp = {
            id:v,
            name:skill.name,
            info:skill.info,
            pic:skill.pic,
            level:user.level,
            max:skill.max,
            unlock:user.unlock,
            condition:{
                skill:user.condition.skill, 
                point:user.condition.point, 
                all_use:user.condition.all_use,
            },
        }

        // 更新当前类中的数据
        DataUtils.assign(this.data,tmp);

        //检查是否解锁及技能
        if(this.checkConditionFill()){
            user.unlock =1;
            this.data.unlock =1;
        }

        return true;
    }
    

    //检查解锁条件是否满足
    checkConditionFill(){
        let cod = this.data.condition;
        let player = PlayerSKills.find(data=>data.id === cod.skill);
        if(player == null)return;
        let skill = ConfigSkills.find(data=> data.id === player.skillId);
        if(player.level >= skill.max){
            return true;
        }else{
            return false;
        }

    }

}
